package com.xiaotou.db.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.xiaotou.db.pojo.WbUser;

/**
 * Data access object (DAO) for domain model class User.
 * @see com.AppUser.db.pojo.User
 * @author MyEclipse - Hibernate Tools
 */
@Repository("wbUserDao")
public class WbUserDAO extends HibernateDaoSupport {

    private static final Log log = LogFactory.getLog(WbUserDAO.class);

    public void save(WbUser user) {

        log.debug("saving User instance");
        try {
            getHibernateTemplate().save(user);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void update(WbUser user) {

        log.debug("saving User instance");
        try {
            getHibernateTemplate().update(user);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(WbUser user) {
        log.debug("deleting User instance");
        try {
            getHibernateTemplate().delete(user);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public WbUser findById(Long id) {
        log.debug("getting User instance with id: " + id);
        try {
            WbUser instance = (WbUser) getHibernateTemplate().get("com.xiaotou.db.pojo.SinaUser", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

	public WbUser findByWbId(final Long wbId) {
        return (WbUser) getHibernateTemplate().execute(new HibernateCallback<Object>() {
            public Object doInHibernate(final Session session) throws HibernateException, SQLException {
                final String sql = "select s from SinaUser s where s.wbId =:wbId";

                final Query query = session.createQuery(sql).setLong("wbId", wbId);
                return query.uniqueResult();
            }
        });
    }

    @SuppressWarnings("unchecked")
    public List<WbUser> find(final Long start, final Integer rows) {
        return (List<WbUser>) getHibernateTemplate().execute(new HibernateCallback<Object>() {
            public Object doInHibernate(final Session session) throws HibernateException, SQLException {
                final String sql = "select s from SinaUser s where s.id > :start ";

                final Query query = session.createQuery(sql).setLong("start", start).setMaxResults(rows);
                return query.list();
            }
        });
    }

}